#!/usr/bin/env python
# -*- coding: utf-8 -*-
# 时间     : 2020/10/11 11:02
# 作者     : DL (584054432@qq.com)
# 网址     : https://gitee.com/dingliangPython/
# 软件版本 : Python3.7.3
# 功能     ：

import tools


class Base(object):
    """docstring for """

    def __init__(self,connect):
        super(Base, self).__init__()
        self.connect =connect

    # 查询数据
    def loadSql(self, sql, returnList = None):

        with self.getCursor() as cursor:
            cursor.execute(sql)
            value = cursor.fetchall()
            data = []
            if value and len(value) >= 1:
                # 返回元组
                if returnList:
                    return value
                # 返回字典
                field = tuple([d[0] for d in cursor.description])
                for v in value:
                    data.append(dict(zip(field, v)))
                return data
            else:
                return 0

    # 执行 插入，更新，删除等操作语句
    @tools.try_error
    def execSql(self, sql, value_list = None):
        with self.getCursor() as cursor:
            if not value_list:
                cursor.execute(sql)
                result = cursor.rowcount
                return result
            else:
                cursor.executemany(sql, value_list)
                result = cursor.rowcount
                return result